package com.ssm.springboot.controller;

import com.ssm.springboot.pojo.DataInfo;
import com.ssm.springboot.pojo.TbUser;
import com.ssm.springboot.pojo.req.TbUserReq;
import com.ssm.springboot.service.TbUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * (TbUserReq)表控制层
 *
 * @author makejava
 * @since 2021-12-27 20:36:33
 */
@Controller
@RequestMapping("/tbUser")
public class TbUserController {
    /**
     * 服务对象
     */
    @Resource
    private TbUserService tbUserService;

    //登录
    @ResponseBody
    @RequestMapping("/login")
    public DataInfo login(@RequestBody TbUserReq tbUserReq, HttpSession session) {
        return tbUserService.login(tbUserReq, session);
    }

    //退出登录
    @RequestMapping("/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/layuimini-master/login.html";
    }

    //    @RequestMapping("/getUser")
//    public DataInfo getUser(){
//        Subject subject = SecurityUtils.getSubject();
//        Object userName = subject.getSession().getAttribute("currentUserId");
//        System.out.println(userName);
//        if(userName!=null){
//            return new DataInfo(0,"查询成功",null,userName);
//        }else {
//            return new DataInfo(1,"没有登录",null,null);
//
//        }
//    }
    //查询所有用户
    @ResponseBody
    @RequiresPermissions(value = {"/tbUser/findAll"})
    @RequestMapping("/findAll")
    public DataInfo findAll(@RequestParam("page") int page, @RequestParam("limit") int limit) {
        return tbUserService.findAll(page, limit);
    }

    @ResponseBody
    @RequiresPermissions(value = {"/tbUser/add"})
    @RequestMapping("/add")
    public DataInfo add(@RequestBody TbUser tbUser) {
        return tbUserService.add(tbUser);
    }

    @ResponseBody
    @RequiresPermissions(value = {"/tbUser/delete"})
    @RequestMapping("/delete")
    public DataInfo delete(Integer id) {
        return tbUserService.delete(id);
    }

}